<?php
/**
 * 广告管理相关操作
 * 2016-9-9 10:41:39
 * @version 1.0
 */
namespace Home\Controller;
class AdmanageController extends PmpController
{
    /** 
     * 首页
     * 2016-9-9 10:41:47
     * @version 1.0
     */
    public function index()
    {
        $adInfoModel = D('AdInfo');
        $adArray = $adInfoModel->getAdInfo();
        foreach ($adArray as $key => $value)
        {
            if ($value['status'] == '1')
            {
                $adArray[$key]['status_img'] = C('URL_POST') . 'Public/img/icons/open_btn.png';
                $adArray[$key]['status_name'] = '启用';
            }
            else
            {
                $adArray[$key]['status_img'] = C('URL_POST') . 'Public/img/icons/close_btn.png';
                $adArray[$key]['status_name'] = '停用';
            }
            if ($value['url'] == null)
            {
                $adArray[$key]['url'] = C('URL_POST') . 'Public/img/icons/un_fresh.png';
            }
            else
            {
                $adArray[$key]['url'] = C('URL_ADMIN_POST') . $value['url'];
            }
        }
        $this->assign("adArray", $adArray);
        $this->display();
    }
    
    /**
     * 添加广告
     * 2016-9-9 10:41:47
     * @version 1.0
     */
    public function add()
    {
        $this->display();
    }
    
    /**
     * 添加
     * 2016-9-23 11:14:39
     * sz
     */
    public function submit_add ()
    {
        $user_id = $_SESSION['hb_think_id'];
        $title = $_POST['title'];
        $weight = $_POST['weight'];
        $url = $_POST['url'];
        $link = $_POST['link'];
        
        $tagUrl = C('URL_ADMIN_POST') . $url;
        $dx_size = $this->remote_filesize($tagUrl);
        $temp_size = $this->get_img_dim($tagUrl);
        $cc_size = $temp_size[0] . "*" . $temp_size[1];
       
        $adInfoModel = D('AdInfo');
        
        // 判断是否添加成功
        $addFlag = $adInfoModel->addAd($user_id, $title, $dx_size, $cc_size, $weight, $url, $link);
        if ($addFlag >= 0)
        {
            $logArray[$this->operationAdd][] = "【广告管理】->【添加广告】";
            $this->addLog($logArray);
            echo "1";
        }
        else
        {
        	echo "0";
        }
    }
    
    /**
     * 修改广告
     * 2016-9-18 14:26:38
     * @version 1.0
     */
    public function modify()
    {
        $id = $_REQUEST['id'];
        $adInfoModel = D('AdInfo');
        $adArray = $adInfoModel->getAdById($id);
        $this->assign("adInfo", $adArray[0]);
        $this->display();
    }
    
    /**
     * 修改
     * 2016-9-23 13:32:28
     * sz
     */
    public function submit_modify ()
    {
        $user_id = $_SESSION['hb_think_id'];
        $id = $_POST['id'];
        $title = $_POST['title'];
        $weight = $_POST['weight'];
        $url = $_POST['url'];
        $link = $_POST['link'];
        $tagUrl = C('URL_ADMIN_POST') . $url;
        $dx_size = $this->remote_filesize($tagUrl);
        $temp_size = $this->get_img_dim($tagUrl);
        $cc_size = $temp_size[0] . "*" . $temp_size[1];
         
        $adInfoModel = D('AdInfo');
    
        // 判断是否添加成功
        $addFlag = $adInfoModel->modifyAd($id, $user_id, $title, $dx_size, $cc_size, $weight, $url, $link);
        if ($addFlag >= 0)
        {
            $logArray[$this->operationAdd][] = "【广告管理】->【添加广告】";
            $this->addLog($logArray);
            echo "1";
        }
        else
        {
            echo "0";
        }
    }
    
    /**
     * 更改状态
     * 2016-9-14 08:59:54
     * @version 1.0
     */
    public function update_status ()
    {
        $id = $_POST['id'];
        $status = $_POST['status'];
        // 判断是否更新成功
        if ($status == 1)
        {
            $status = 0;
        }
        elseif ($status == 0)
        {
            $status = 1;
        }
        $adInfoModel = D('AdInfo');
        $updateFlag = $adInfoModel->updateStatus($id, $status);
        if ($updateFlag >= 0)
        {
            $logArray[$this->operationModify][] = "【广告管理】->【更改状态】";
            $this->addLog($logArray);
            echo "1";
        }
        else
        {
            echo "0";
        }
    }
    
    /**
     * 删除
     * 2016-9-14 09:17:25
     * sz
     */
    public function del ()
    {
        $id = $_POST['id'];
        $adInfoModel = D('AdInfo');
        // 判断是否删除成功
        $delFlag = $adInfoModel->del($id);
        if ($delFlag >= 0)
        {
            $logArray[$this->operationDelete][] = "【广告管理】->【删除广告】";
            $this->addLog($logArray);
            echo "1";
        }
        else
        {
            echo "0";
        }
    }
}